package com.zzx.boot.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.zzx.boot.common.BaseContext;
import com.zzx.boot.common.CustomException;
import com.zzx.boot.common.R;
import com.zzx.boot.pojo.ShoppingCart;
import com.zzx.boot.service.ShoppingCartService;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

@Controller
@ResponseBody
@RequestMapping("/shoppingCart")
public class ShoppingCartController {
    @Resource
    private ShoppingCartService shoppingCartService;

    @PostMapping("/add")
    public R<ShoppingCart> add(@RequestBody ShoppingCart shoppingCart){
        //获取当前用户的id
        Long currentId = BaseContext.getCurrentId();
        //设置当前用户的id
        shoppingCart.setUserId(currentId);
        ShoppingCart one = null;
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper();
        //判断当前传入的是套餐还是菜品
        queryWrapper.eq(ShoppingCart::getUserId,shoppingCart.getUserId());
        //判断是否为菜品
        if (shoppingCart.getDishId()!=null){
            //判断当前用户是否有记录
//            queryWrapper.eq(ShoppingCart::getUserId,shoppingCart.getUserId());
            //判断菜品id是否存在
            queryWrapper.eq(ShoppingCart::getDishId,shoppingCart.getDishId());
            queryWrapper.orderByAsc(ShoppingCart::getCreateTime);
            //查询表中是否存在该记录 并且口味是否相同
//            queryWrapper.eq(StringUtils.isNotEmpty(shoppingCart.getDishFlavor()),ShoppingCart::getDishFlavor,shoppingCart.getDishFlavor());
            //查询表中是否有记录
              one = shoppingCartService.getOne(queryWrapper);

        }else{
//            //查询用户的id
//            queryWrapper.eq(ShoppingCart::getUserId,shoppingCart.getUserId());
            //查询套餐id
            queryWrapper.eq(ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
              one = shoppingCartService.getOne(queryWrapper);

        }
        if (one!=null){
            one.setNumber(one.getNumber()+1);
            shoppingCartService.updateById(one);
        }else{
            //如果没有记录
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCart.setNumber(1);
            boolean save = shoppingCartService.save(shoppingCart);
            one = shoppingCart;
        }
        return R.success(one);
    }

    @GetMapping("/list")
    public R<List<ShoppingCart>> list(){
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.eq(ShoppingCart::getUserId,BaseContext.getCurrentId());
        //获取当前登录用户的购物车信息
        List<ShoppingCart> list = shoppingCartService.list(queryWrapper);
        return R.success(list);
    }

    @PostMapping("/sub")
    public R<ShoppingCart> sub(@RequestBody ShoppingCart shoppingCart){
        //两个条件
        //一个当前用户的id
        Long currentId = BaseContext.getCurrentId();
        shoppingCart.setUserId(currentId);
        //当前菜品的id
        System.out.println(shoppingCart.getDishId());

        LambdaUpdateWrapper<ShoppingCart> queryWrapper = new LambdaUpdateWrapper();
        queryWrapper.eq(ShoppingCart::getUserId,shoppingCart.getUserId());
       //判断当前是套餐还是菜品
        if (shoppingCart.getDishId()!=null){
            queryWrapper.eq(ShoppingCart::getDishId,shoppingCart.getDishId());
        }else {
            queryWrapper.eq(ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
        }

        //查询原来的字段
        ShoppingCart one = shoppingCartService.getOne(queryWrapper);
        LambdaQueryWrapper<ShoppingCart> queryWrapper1 = new LambdaQueryWrapper();
        if (one!=null) {
           if (one.getNumber()-1>=0){
               one.setNumber(one.getNumber()-1);
               queryWrapper1.eq(one.getDishId()!=null,ShoppingCart::getDishId,one.getDishId());
               queryWrapper1.eq(one.getSetmealId() !=null, ShoppingCart::getSetmealId, one.getSetmealId());
           }else {
              throw  new CustomException("无法再继续减少了");
           }
           if (one.getNumber()<=0){
               shoppingCartService.remove(queryWrapper1);
               one.setNumber(0);
           }else {
               queryWrapper.set(ShoppingCart::getNumber, one.getNumber());
               shoppingCartService.update(queryWrapper);
           }

        }

        return R.success(one);
    }

    @DeleteMapping("/clean")
    public R<String> clean(){
        //通过用户的id去删除所有的数据
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.eq(ShoppingCart::getUserId,BaseContext.getCurrentId());
        boolean remove = shoppingCartService.remove(queryWrapper);
        return R.success("清空购物车成功");
    }

}
